package com.example.demo.system.controller;

import com.example.demo.common.QrCodeCreateUtil;
import com.example.demo.common.RedisUtil;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lx
 */
@Controller
@RequestMapping("/login")
public class QrCodeLoginController {
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping(value = "/loginQrCode.json")
    @ResponseBody
    public Map<String,Object> loginQrCode(){
        Map<String,Object> data=new HashMap<>();
        data.put("code",0);
        data.put("msg","获取登录二维码失败！");
        try{
            String imgName=+System.currentTimeMillis()+".png";
            File files = new File("/Volumes/work/upload/"+imgName);
            FileOutputStream fileOutputStream=new FileOutputStream(files);
            Map<String,String> res=QrCodeCreateUtil.createQrCode(fileOutputStream,"/login/loginInfo.json?token=112233",200,"png");
            if ("1".equals(res.get("status"))){
                data.put("msg","获取登录二维码成功！");
                data.put("code",1);
                data.put("src","/img/"+imgName);
                data.put("token","112233");
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
            return data;
        }
    }

    @GetMapping(value = "/loginInfo.json")
    @ResponseBody
    public Map<String,Object> loginInfo(@Param("token") String token){
        Map<String,Object> data=new HashMap<>();
        data.put("code",0);
        data.put("msg","扫描失败！");
        try{
            if(token!=null&&!"".equals(token)){
                data.put("code",1);
                redisUtil.hPut("login",token,"1");
                data.put("msg","扫描成功！");

            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
            return data;
        }
    }

    @GetMapping(value = "/getLoginStatus.json")
    @ResponseBody
    public Map<String,Object> getLoginStatus(@Param("token") String token){
        Map<String,Object> data=new HashMap<>();
        data.put("code",0);
        data.put("msg","未登录！");
        try{
            if(token!=null&&!"".equals(token)){
                Object check=redisUtil.hGet("login",token);
                if (check!=null&&!"".equals(check)){
                    data.put("code",1);
                    data.put("msg","登录成功！");
                }
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
            return data;
        }
    }
}
